package com.example.shop.controller;

import com.example.shop.bean.CommentBean;
import com.example.shop.bean.ProductBean;
import com.example.shop.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentMapper commentMapper;
    //添加

    @ResponseBody
    @RequestMapping("/add")
    public String addComment(CommentBean bean){
        bean.ctime=new Date();
        try{
            commentMapper.insert(bean);
            return "1";
        }catch (Exception e){
            System.out.println(e);
            return "0";
        }

    }

    @ResponseBody
    @RequestMapping("/list")
    public List<CommentBean> listComment(Integer pid){
        List<CommentBean> lc=new ArrayList<>();
        try{
            lc=commentMapper.listComment(pid);
        }catch (Exception e){
            System.out.println("列出评论出错");
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for(CommentBean bean:lc){
            bean.ftime=sdf.format(bean.ctime);
        }
        return lc;
    }

    @ResponseBody
    @RequestMapping("/list2")
    public List<CommentBean> listComment2(){
        List<CommentBean> lc=new ArrayList<>();
        try{
            lc=commentMapper.listComment2();
        }catch (Exception e){
            System.out.println("列出评论出错");
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for(CommentBean bean:lc){
            bean.ftime=sdf.format(bean.ctime);
        }
        return lc;
    }

    @RequestMapping("/del")
    public String comDel(Integer id){
        try{
            commentMapper.deleteById(id);
            return "redirect:/vx/comment";
        }catch (Exception e){
            System.out.println("删除评论失败");
            return "redirect:/vx/comment";
        }
    }
}
